<?php
/**
 * 工具类，提供加密解密等方法
 * @author Administrator
 *
 */
class tripledes{
	private static $key = "renooos";
	private static function Encrypt($key,$text){  
		$cipher = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');  
		srand((double)microtime() * 1000000);  
		$size=mcrypt_enc_get_iv_size($cipher);  
		$iv = mcrypt_create_iv($size, MCRYPT_RAND);  
		if(mcrypt_generic_init($cipher, $key, $iv) != -1){  
		$cipherText = mcrypt_generic($cipher,$text);  
		mcrypt_generic_deinit($cipher);  
		return bin2hex($cipherText);  
		}  
	}  
	
	private static function Decrypt($key,$encryptedText){  
		$cipherText=self::Hex2bin($encryptedText);  
		$cipher = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');  
		srand((double)microtime() * 1000000);  
		$size=mcrypt_enc_get_iv_size($cipher);  
		$iv = mcrypt_create_iv($size, MCRYPT_RAND);  
		if (mcrypt_generic_init($cipher, $key, $iv) != -1){  
		$decrypted_data = mdecrypt_generic($cipher,$cipherText);  
		mcrypt_generic_deinit($cipher);  
		return $decrypted_data;  
		}  
	}  
	private static function Hex2bin($h){  
		if (!is_string($h)) return null;  
		$r='';  
		for ($a=0; $a<strlen($h); $a+=2) {  
			$r.=chr(hexdec($h{$a}.$h{($a+1)}));   
		}  
		return $r;  
	}
	/**
	 * des加密文件
	 * */
	public static function DesE($file){
		$text=file_get_contents($file);
		$deseText=self::Encrypt(self::$key, $text);
		file_put_contents($file, $deseText);		
	}
	
	/**
	 * des解密文件
	 * */
	public static function DesD($file){
		$text=file_get_contents($file);
		$desDText=self::Decrypt(self::$key, $text);
		file_put_contents($file, $desDText);
	}
}



